package com.zmn.oms.task.job.attendance;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.base.common.data.common.constant.TagConsts;
import com.zmn.base.common.data.common.dro.tags.TagsDRO;
import com.zmn.base.common.data.common.query.tags.TagsQuery;
import com.zmn.base.common.data.dubbo.interfaces.tags.TagsListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.business.interfaces.attendance.AttendanceService;
import com.zmn.oms.business.interfaces.base.BaseCodeService;
import com.zmn.oms.manager.utils.TaskConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: huangyifeng
 * @description: 健康考勤
 * @create: 2020-02-11 16:56
 **/
@LTS
@Slf4j
public class AttendanceJob {

    @Autowired
    private AttendanceService attendanceService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private TagsListRemoteService tagsListRemoteService;
    @Resource
    TaskManager taskManager;
    @Autowired
    BaseCodeService baseCodeService;

    /**
     * 每天早上7点执行
     */
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.HEALTHY_CARD_PUNCHING);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.HEALTHY_CARD_PUNCHING);
//        job.setCronExpression("0 0/1 * * * ? ");
        job.setCronExpression("0 0 7 1/1 * ? ");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#oms#healthcardpunching# job submit fail...");
        }
    }

    /**
     * 每天早上7点执行APP推送健康打卡
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.HEALTHY_CARD_PUNCHING)
    public Result runAttendanceJob(Job job) {
        TagsQuery tagsDIO = new TagsQuery();
        tagsDIO.setPlat(GlobalConsts.PLAT_MARK_ZMN);
        tagsDIO.setOneTypeId(TagConsts.TAG_ONE_TYPE_HEALTH_PUSH);
        ResponseDTO<List<TagsDRO>> listResponseDTO = tagsListRemoteService.listByTagsDIO(tagsDIO);
        boolean heathPushOn = listResponseDTO.isSuccess() && (listResponseDTO.getData() != null && listResponseDTO.getData().stream().anyMatch(e -> Objects.equals(e.getStatus(), GlobalConsts.YES)));
        if (!heathPushOn) {
            log.info("【{}】<健康考勤> 该功能没有开启。", AttendanceConsts.HEALTH_ATTENDANCE);
            return null;
        }

        Date startDate = DateUtil.getNow();

        // 推送信息
        attendanceService.pushMasterHealthAttendance();

        Date endDate = DateUtil.getNow();
        String startDateStr = DateUtil.dateFormatToString(startDate, DateUtil.FORMAT_DEFAULT);
        String endDateStr = DateUtil.dateFormatToString(endDate, DateUtil.FORMAT_DEFAULT);

        log.info("#oms#attendance_pushAPPJob# 推送健康考勤任务执行成功 ！{}-{}", startDateStr, endDateStr);
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
